package com.artpri.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.artpri.model.AdPicture;
import com.artpri.model.ShopkeeperInfo;
import com.artpri.model.vo.RecGoods;
import com.artpri.model.vo.SimpleGoods;
import com.artpri.service.AdPictureService;
import com.artpri.util.RandomUtil;

/**
 * 
 * @author ZhangSi
 *
 */
public class AdPictureServiceImpl extends BaseService implements AdPictureService{

	@Override
	@Transactional(readOnly=true)
	public Map<String, AdPicture> getAdPictureMapByPlate(String plateName, ImageState state) {
		Map<String, AdPicture> map = null;
		List<AdPicture> list = getAdPictureDao().findAdPicturesByPlate(plateName, state);

		if (list != null) {
			map = new HashMap<>();
			for (Iterator<AdPicture> it = list.iterator(); it.hasNext();) {
				AdPicture ad = (AdPicture) it.next();
				map.put(ad.getApPositionName(), ad);
			}
		}
		
		return map;
	}
	
	@Override
	@Transactional(readOnly=true)
	public List<RecGoods> getRecWorksByPlate(String plateName, ImageState state) {
		List<RecGoods> list = null;
		List<AdPicture> adList = getAdPictureDao().findAdPicturesByPlate(plateName, state);
		
		if (adList != null) {
			list = new ArrayList<>();
			RecGoods rec = null;
			for (Iterator<AdPicture> it = adList.iterator(); it.hasNext();) {
				AdPicture adPicture = (AdPicture) it.next();
				rec = getRecGoodsDao().get(adPicture.getApGoodNum());
				list.add(rec);
			}
		}
		
		return list;
	}

	@Override
	@Transactional(readOnly=true)
	public Map<String, RecGoods> getRecWorkMapByPlate(String plateName, ImageState state) {
		List<AdPicture> list = getAdPictureDao().findAdPicturesByPlate(plateName, state);
		Map<String, RecGoods> map = null;
		
		if (list != null) {
			map = new HashMap<>();
			RecGoods rec = null;
			for (Iterator<AdPicture> it = list.iterator(); it.hasNext();) {
				AdPicture adPicture = (AdPicture) it.next();
				rec = getRecGoodsDao().get(adPicture.getApGoodNum());
				map.put(adPicture.getApPositionName(), rec);
			}
		}
		
		return map;
	}
	
	@Override
	@Transactional(readOnly=true)
	public Map<String, SimpleGoods> getRecGoodsMapByPlate(String plateName, ImageState state) {
		List<AdPicture> list = getAdPictureDao().findAdPicturesByPlate(plateName, state);
		Map<String, SimpleGoods> map = null;
		
		if (list != null) {
			map = new HashMap<>();
			for (Iterator<AdPicture> it = list.iterator(); it.hasNext();) {
				AdPicture adPicture = (AdPicture) it.next();
				SimpleGoods sg = getSimpleGoodsDao().get(adPicture.getApGoodNum());
				map.put(adPicture.getApPositionName(), sg);
			}
		}
		
 		return map;
	}
	
	@Override
	@Transactional(readOnly=true)
	public Map<String, ShopkeeperInfo> getRecStoreMapByPlate(String plateName, ImageState state) {
		List<AdPicture> list = getAdPictureDao().findAdPicturesByPlate(plateName, state);
		Map<String, ShopkeeperInfo> map = null;

		if (list != null) {
			map = new HashMap<>();
			for (Iterator<AdPicture> it = list.iterator(); it.hasNext();) {
				AdPicture adPicture = (AdPicture) it.next();
				ShopkeeperInfo shop = getShopkeeperDao().get(ShopkeeperInfo.class, adPicture.getApGoodNum());
				map.put(adPicture.getApPositionName(), shop);
			}
		}
		
		return map;
	}
	
	@Override
	@Transactional(readOnly=true)
	public Map<String, List<SimpleGoods>> getBuyWhatMapByPlate(String plateName, ImageState state, int num) {
		List<AdPicture> list = getAdPictureDao().findAdPicturesByPlate(plateName, state);
		Map<String, List<SimpleGoods>> map = null;
		
		if(list != null) {
			map = new HashMap<>();
			for (Iterator<AdPicture> it = list.iterator(); it.hasNext();) {
				AdPicture adPicture = (AdPicture) it.next();
				List<Integer> goodsNumList = getGoodsDao().findGoodsNumsByShop(adPicture.getApGoodNum());
				int size = goodsNumList != null ? goodsNumList.size() : 0;
				List<SimpleGoods> goodsList = new ArrayList<>();
				if (size != 0) {
					int[] a = RandomUtil.getRangeRandom(0, size, num, false);
					for (int i = 0; i < a.length; i++) {
						SimpleGoods sg = getSimpleGoodsDao().findSimpleGoodsToBuyWhat(goodsNumList.get(a[i]));
						goodsList.add(sg);
					}
				}
				map.put(adPicture.getApPositionName(), goodsList);
			}
		}
		
		return map;
	}

	@Override
	@Transactional(readOnly=true)
	public List<SimpleGoods> getGoodsDetailRecWorkList(String plateNum, int num) {
		List<Integer> goodsNumList = getRecommendWorkDao().getGoodsNumByPlate(plateNum);
		
		List<SimpleGoods> list = null;
		
		if (goodsNumList != null && goodsNumList.size() != 0) {
			list = new ArrayList<>();
			int[] ai = RandomUtil.getRangeRandom(0, goodsNumList.size(), num, false);
			
			for (int i = 0; i < ai.length; i++) {
				SimpleGoods sg = getSimpleGoodsDao().get(goodsNumList.get(ai[i]));
				list.add(sg);
			}
		}
		
		return list;
	}
}
